package com.shire.shire_interface.constants;

import com.shire.shire_interface.common.utils.KeyValue;
import com.shire.shire_interface.common.utils.MathUtils;
import com.shire.shire_interface.common.utils.StrUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

public class IphoneConfig {
    private static Logger logger = LoggerFactory.getLogger(IphoneConfig.class);

    private String configName;
    private List<IphoneConfigItem> _config_items = new ArrayList<IphoneConfigItem>();
    private Map<String, List<Map<String, Object>>> _config_versions = new HashMap<String, List<Map<String, Object>>>();

    public boolean init(String configName, Map<String, Object> map){
        this.configName = configName;
        List<IphoneConfigItem> tmp_config_items = new ArrayList<IphoneConfigItem>();
        Map<String, List<Map<String, Object>>> tmp_config_versions = new HashMap<String, List<Map<String, Object>>>();
        try{
            List<Map<String, Object>> deviceList = (List<Map<String,Object>>)map.get("items");
            for(Map<String, Object> subMap : deviceList){
                IphoneConfigItem iphoneItem = new IphoneConfigItem();
                iphoneItem.setName((String)subMap.get("name"));
                iphoneItem.setRate((Integer)subMap.get("rate"));
                List<String> osversion = (List<String>)subMap.get("osversion");
                for(String os : osversion){
                    String[] eles = os.split(":");
                    iphoneItem.getOsversions().add(new KeyValue<String, Integer>(eles[0], StrUtils.getIntValue(eles[1], 0)));
                }
                tmp_config_items.add(iphoneItem);
            }
            tmp_config_versions = (Map<String, List<Map<String, Object>>>)map.get("versions");

            _config_items = tmp_config_items;
            _config_versions = tmp_config_versions;
            return true;
        }
        catch(Exception e){
            logger.error("载入手机型号、版本分布异常【configName="+configName+"】：", e);
        }
        return false;
    }

    public KeyValue<String, String> getRandomConfig(){
        int rand = new Random().nextInt(100);
        try{
            IphoneConfigItem item = null;
            for(IphoneConfigItem tmpItem : _config_items){
                if(rand<=tmpItem.getRate()){
                    item = tmpItem;
                    break;
                }
                rand -= tmpItem.getRate();
            }
            if(item!=null){
                String prefixVersion = item.getOsVersion();
                if(StrUtils.isNotBlank(prefixVersion)){
                    List<Map<String, Object>> versionList = (List<Map<String, Object>>)_config_versions.get(prefixVersion);
                    if(versionList!=null){
                        rand = new Random().nextInt(100);
                        String version = null;
                        for(Map<String, Object> map : versionList){
                            int tmpRate = (Integer)map.get("r");
                            if(rand<=tmpRate){
                                version = (String)map.get("v");
                                break;
                            }
                            rand -= tmpRate;
                        }
                        if(StrUtils.isNotBlank(item.getName()) && StrUtils.isNotBlank(version)){
                            return new KeyValue<String, String>(item.getName(), version);
                        }
                    }
                }
            }
        }
        catch(Exception e){
            logger.error("【随机生成参数】异常[configName="+configName+"]:", e);
        }
        return null;
    }

    public void testRate(){
        int total = 10000;
        Map<String, Integer> typeMap = new HashMap<String, Integer>();
        Map<String, Integer> osMap = new HashMap<String, Integer>();
        try{
            for(IphoneConfigItem item : _config_items){
                int typeNum = MathUtils.getNullValue(typeMap.get(item.getName()), 0);
                int num = item.getRate() * 100;
                typeMap.put(item.getName(), typeNum+num);
                for(KeyValue<String, Integer> kv : item.getOsversions()){
                    int osNum = MathUtils.getNullValue(osMap.get(kv.getKey()), 0);
                    int tmpNum = num * kv.getValue() / 100;
                    osMap.put(kv.getKey(), osNum+tmpNum);
                }
            }

            for(Map.Entry<String, Integer> entry : typeMap.entrySet()){
                System.out.println(entry.getKey()+":"+entry.getValue()/100);
            }
            System.out.println("=================================");
            for(Map.Entry<String, Integer> entry : osMap.entrySet()){
                System.out.println(entry.getKey()+":"+entry.getValue()/100);
            }
        }
        catch(Exception e){

        }
    }
}
